package cn.lycode.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.lycode.common.core.controller.BaseController;
import cn.lycode.common.core.domain.AjaxResult;
import cn.lycode.common.core.domain.model.SelectIdsDTO;
import cn.lycode.common.core.page.PageResult;
import cn.lycode.system.domain.dto.sysdict.SysDictAddDTO;
import cn.lycode.system.domain.dto.sysdict.SysDictListDTO;
import cn.lycode.system.domain.dto.sysdict.SysDictUpDTO;
import cn.lycode.system.domain.entity.SysDict;
import cn.lycode.system.domain.vo.DictCustomVO;
import cn.lycode.system.service.SysDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 * @since 2023-08-18
 */
@RestController
@RequestMapping("/sys-dict")
@RequiredArgsConstructor
public class SysDictController extends BaseController {

    private final SysDictService sysDictService;

    /**
     * 系统字典查询-全部
     */
    @SaIgnore
    @GetMapping("/all")
    public AjaxResult<Map<String, List<DictCustomVO>>> listDict() {
        return AjaxResult.success(sysDictService.dictAll());
    }


    @SaCheckPermission(value = "system:dict:list")
    @PostMapping("/list")
    public AjaxResult<PageResult<SysDict>> list(@RequestBody @Valid SysDictListDTO dto) {
        return AjaxResult.success(sysDictService.list(dto));
    }

    /**
     * *********************************增删改查*******************************************
     */

    @SaCheckPermission(value = "system:dict:add")
    @PostMapping("/add")
    public AjaxResult<Object> add(@Valid @RequestBody SysDictAddDTO dto) {
        return toAjax(sysDictService.add(dto));
    }

    @SaCheckPermission(value = "system:dict:update")
    @PostMapping("/update")
    public AjaxResult<Object> update(@Valid @RequestBody SysDictUpDTO dto) {
        return toAjax(sysDictService.update(dto));
    }

    @SaCheckPermission(value = "system:dict:lock")
    @PostMapping("/lock")
    public AjaxResult<Object> lock(@RequestBody SelectIdsDTO dto) {
        return toAjax(sysDictService.lock(dto));
    }

    @SaCheckPermission(value = "sys:dict:delete")
    @PostMapping("/delete")
    public AjaxResult<Object> remove(@RequestBody SelectIdsDTO dto) {
        return toAjax(sysDictService.delete(dto));
    }


}
